<template>
  <view class="commonLim-list">
    <view class="searchclass">
      <view class="left">
        <up-search
          v-model="query.trackingNo"
          placeholder="请输入随工单编号"
          :clearabled="true"
          :showAction="false"
          @search="handleSearch"
        />
        <view v-show="isshow1" class="left">
          <div @click="handlefocus1">
            <up-search
              v-model="query.materialTypeName"
              placeholder="请选择材质"
              :clearabled="true"
              :showAction="false"
              disabled
            />
          </div>
          <div @click="handlefocus2">
            <up-search
              v-model="query.specificationName"
              placeholder="请选择规格型号"
              :clearabled="true"
              :showAction="false"
              disabled
            />
          </div>
        </view>
      </view>
      <view class="right">
        <up-button
          type="primary"
          @click="handleSearch"
          size="small"
          shape="circle"
          class="first"
          >搜索</up-button
        >
        <up-button
          type="primary"
          @click="handlecancel"
          size="small"
          shape="circle"
          >重置</up-button
        >
      </view>
    </view>
    <view class="upCcon">
      <up-icon
        :name="iconName"
        size="50rpx"
        color="#3c9cff"
        @click="handleicon"
      ></up-icon>
    </view>
    <scroll-view
      class="commonLim-listView"
      scroll-y
      @scrolltolower="onScrollToBottom"
    >
      <view v-show="state.rows.length">
        <view
          v-for="(item, index) in state.rows"
          :key="item.id"
          class="commonLim-listView-item"
        >
          <view class="commonLim-listView-item-title">
            <label>{{ item.batchNo }}</label>
            <text class="mgr40">工序：{{ item.processName }}</text>
            <text>序号：{{ index + 1 }}</text>
          </view>
          <view class="commonLim-listView-item-content">
            <view class="half width100">
              <label>随工单编号:</label>
              <text>{{ item.trackingNo }}</text>
            </view>
            <view class="half width100">
              <label>物料编码:</label>
              <text>{{ item.materialCode }}</text>
            </view>
            <view class="half width100">
              <label>材质:</label>
              <text>{{ item.materialTypeName }}</text>
            </view>
            <view class="half width100">
              <label>规格型号:</label>
              <text>{{ item.specificationName }}</text>
            </view>
            <view class="half width100">
              <label>炉号:</label>
              <text>{{ item.heatNo }}</text>
            </view>
            <view class="half width100">
              <label>机台编号:</label>
              <text>{{ item.machineNumber }}</text>
            </view>
            <view class="half width100">
              <label>分轮编号:</label>
              <text>{{ item.roundNo }}</text>
            </view>
            <view class="half width100">
              <label>分轮重量:</label>
              <text>{{ item.roundWeight }}</text>
            </view>
          </view>
          <view class="commonLim-listView-item-button">
            <view class="box">
              <up-button
                type="error"
                text="打印标签"
                size="small"
                @click="handleprint(item)"
              ></up-button>
              <up-button
                type="primary"
                text="作废"
                size="small"
                @click="handlevoid(item)"
              ></up-button>
            </view>
          </view>
        </view>
      </view>
      <up-empty mode="data" v-show="state.rows.length == 0"> </up-empty>
      <up-loadmore
        v-if="state.rows.length"
        :status="loadStatus"
        :icon="true"
        loadmore-text="轻轻上拉获取更多"
        loading-text="努力加载中..."
        nomore-text="没有更多了"
        @loadmore="handleloadMore"
      />
    </scroll-view>
    <PrintLabel
      @closeFn="showTemReject = false"
      :showTemReject="showTemReject"
      :currentObject="currentObject"
    />
  </view>
</template>

<script setup>
import { managementPage, confirmvoid } from "./api.js";
import { useMemberStore } from "@/stores/modules/member";
const memberStore = useMemberStore();
const loadStatus = ref("loadmore");
const iconName = ref("arrow-down");
const showTemReject = ref(false);
const currentObject = ref(null);
const isshow1 = computed(() => {
  if (iconName.value == "arrow-down") {
    return false;
  } else {
    return true;
  }
});
const handleicon = () => {
  if (iconName.value == "arrow-down") {
    iconName.value = "arrow-up";
  } else {
    iconName.value = "arrow-down";
  }
};
const query = ref({
  page: 0,
  size: 10,
});
const state = reactive({
  rows: [],
  totals: 0,
});
const loadData = async () => {
  const res = await managementPage(query.value);
  state.rows = [...state.rows, ...res.content];
  state.totals = res.totalElements;
  if (state.rows.length < state.totals) {
    loadStatus.value = "loadmore"; // 仍可继续上拉
  } else {
    loadStatus.value = "nomore"; // 已全部加载
  }
};
const Consistentlogic = async () => {
  try {
    uni.showLoading({
      title: "加载中",
    });
    state.rows = [];
    state.totals = 0;
    await loadData();
  } finally {
    uni.hideLoading();
  }
};
const handleSearch = () => {
  query.value.page = 0;
  Consistentlogic();
};
const handlecancel = () => {
  query.value = {
    page: 0,
    size: 10,
  };
  Consistentlogic();
};
const handleloadMore = async () => {
  // 防止重复请求
  if (loadStatus.value === "loading") return;
  loadStatus.value = "loading";
  // 判断是否还有更多数据
  if (state.rows.length < state.totals) {
    loadStatus.value = "loadmore"; // 仍可继续上拉
    query.value.page += 1; // 为下一次请求准备页码
    try {
      await loadData();
    } catch (error) {
      console.log("加载数据失败", error);
    }
  } else {
    loadStatus.value = "nomore"; // 已全部加载
  }
};
function onScrollToBottom() {
  // 当 scroll-view 触底且状态为 loadmore 时手动调用
  if (loadStatus.value === "loadmore") {
    handleloadMore();
  }
}

const handlevoid = async (item) => {
  try {
    uni.showLoading({
      title: "加载中",
    });
    await confirmvoid(item.id, memberStore.userName);
    uni.showToast({
      icon: "success",
      title: "作废成功",
    });
    handleSearch();
  } finally {
    uni.hideLoading();
  }
};

const handlefocus1 = () => {
  uni.navigateTo({
    url: `/pages/searchcaizhi/searchcaizhi?from=caizhi`,
  });
};
const handlefocus2 = async () => {
  if (query.value.materialTypeId) {
    uni.navigateTo({
      url: `/pages/searchcaizhi/searchcaizhi?from=guigexinghao&pId=${query.value.materialTypeId}`,
    });
  } else {
    uni.showToast({
      icon: "none",
      title: "请选择材质",
    });
  }
};
const handleprint = (item) => {
  showTemReject.value = true;
  currentObject.value = item;
};
onShow(() => {
  if (memberStore.from_Page == "caizhi") {
    query.value.materialTypeId = memberStore.zemId;
    query.value.materialTypeName = memberStore.zemName;
    query.value.specificationId = "";
    query.value.specificationName = "";
  } else if (memberStore.from_Page == "guigexinghao") {
    query.value.specificationId = memberStore.zemId;
    query.value.specificationName = memberStore.zemName;
  }
  memberStore.zemId = "";
  memberStore.zemName = "";
  memberStore.from_Page = "";
  handleSearch();
});
</script>

<style lang="scss" scoped>
</style>